Passed
Push — dev ( 22baca...1bf816 )
by
unknown
04:47 queued 10s
created

jobBuilderHooks.ts ➔ useLoadClassifications   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 17
rs 9.65
c 0
b 0
f 0
cc 2
1
import { useEffect, useState } from "react";
2
import { useSelector } from "react-redux";
3
import { DispatchType } from "../configureStore";
4
import {
5
  Classification,
6
  Criteria,
7
  Department,
8
  Job,
9
  JobPosterKeyTask,
10
  Skill,
11
} from "../models/types";
12
import {
13
  getDepartments,
14
  departmentsIsLoading,
15
} from "../store/Department/deptSelector";
16
import { getDepartments as fetchDepartments } from "../store/Department/deptActions";
17
import {
18
  getCriteriaByJob,
19
  getCriteriaForJobIsUpdating,
20
  getJob,
21
  getJobIsUpdating,
22
  getTasksByJob,
23
  getTasksForJobIsUpdating,
24
} from "../store/Job/jobSelector";
25
import { RootState } from "../store/store";
26
import {
27
  fetchCriteria,
28
  fetchJob,
29
  setSelectedJob,
30
} from "../store/Job/jobActions";
31
import { getSkills, getSkillsUpdating } from "../store/Skill/skillSelector";
32
import { fetchSkills } from "../store/Skill/skillActions";
33
import {
34
  getClassifications,
35
  classificationsIsLoading,
36
} from "../store/Classification/classificationSelector";
37
import { loadClassificationsIntoState } from "../store/Classification/classificationActions";
38
39
export function useLoadJob(
40
  jobId: number | null,
41
  dispatch: DispatchType,
42
): { job: Job | null; isLoadingJob: boolean } {
43
  const job = useSelector((state: RootState) =>
44
    jobId !== null ? getJob(state, { jobId }) : null,
45
  );
46
  const isLoadingJob = useSelector((state: RootState) =>
47
    jobId !== null ? getJobIsUpdating(state, jobId) : false,
48
  );
49
  useEffect(() => {
50
    if (jobId !== null && job === null && !isLoadingJob) {
51
      dispatch(fetchJob(jobId));
52
    }
53
  }, [jobId, job, isLoadingJob, dispatch]);
54
  useEffect(() => {
55
    dispatch(setSelectedJob(jobId));
56
  }, [jobId, dispatch]);
57
  return { job, isLoadingJob };
58
}
59
60
export function useLoadTasks(
61
  jobId: number | null,
62
  dispatch: DispatchType,
63
): { tasks: JobPosterKeyTask[]; isLoadingTasks: boolean } {
64
  const tasks = useSelector((state: RootState) =>
65
    jobId !== null ? getTasksByJob(state, { jobId }) : [],
66
  );
67
  const isLoadingTasks = useSelector((state: RootState) =>
68
    jobId !== null ? getTasksForJobIsUpdating(state, jobId) : false,
69
  );
70
  const [hasFetchedTasks, setHasFetchedTasks] = useState(false);
71
  useEffect((): (() => void) => {
72
    let isSubscribed = true;
73
    if (jobId && tasks.length === 0 && !isLoadingTasks && !hasFetchedTasks) {
74
      setHasFetchedTasks(true);
75
      dispatch(fetchJob(jobId)).catch((): void => {
76
        if (isSubscribed) {
77
          setHasFetchedTasks(false);
78
        }
79
      });
80
    }
81
    return (): void => {
82
      isSubscribed = false;
83
    };
84
  }, [jobId, tasks.length, isLoadingTasks, hasFetchedTasks, dispatch]);
85
  return { tasks, isLoadingTasks };
86
}
87
88
export function useLoadCriteria(
89
  jobId: number | null,
90
  dispatch: DispatchType,
91
): {
92
  criteria: Criteria[];
93
  isLoadingCriteria: boolean;
94
} {
95
  const criteria = useSelector((state: RootState) =>
96
    jobId ? getCriteriaByJob(state, { jobId }) : [],
97
  );
98
  const isLoadingCriteria = useSelector((state: RootState) =>
99
    jobId ? getCriteriaForJobIsUpdating(state, jobId) : false,
100
  );
101
  const [hasFetchedCriteria, setHasFetchedCriteria] = useState(false);
102
  useEffect((): (() => void) => {
103
    let isSubscribed = true;
104
    if (
105
      jobId &&
106
      criteria.length === 0 &&
107
      !isLoadingCriteria &&
108
      !hasFetchedCriteria
109
    ) {
110
      setHasFetchedCriteria(true);
111
      dispatch(fetchCriteria(jobId)).catch((): void => {
112
        if (isSubscribed) {
113
          setHasFetchedCriteria(false);
114
        }
115
      });
116
    }
117
    return (): void => {
118
      isSubscribed = false;
119
    };
120
  }, [jobId, criteria.length, isLoadingCriteria, hasFetchedCriteria, dispatch]);
121
  return { criteria, isLoadingCriteria };
122
}
123
124
export function useLoadDepartments(
125
  dispatch: DispatchType,
126
): {
127
  departments: Department[];
128
  isLoadingDepartments: boolean;
129
} {
130
  const departments = useSelector(getDepartments);
131
  const isLoading = useSelector(departmentsIsLoading);
132
  useEffect((): void => {
133
    if (departments.length === 0 && !isLoading) {
134
      dispatch(fetchDepartments());
135
    }
136
  }, [departments.length, isLoading, dispatch]);
137
  return { departments, isLoadingDepartments: isLoading };
138
}
139
140
export function useLoadClassifications(
141
  dispatch: DispatchType,
142
): {
143
  classifications: Classification[];
144
  isLoadingClassifications: boolean;
145
} {
146
  const classifications = useSelector(getClassifications);
147
  const isLoading = useSelector(classificationsIsLoading);
148
149
  useEffect((): void => {
150
    if (classifications.length === 0 && !isLoading) {
151
      dispatch(loadClassificationsIntoState());
152
    }
153
  }, [classifications.length, isLoading, dispatch]);
154
155
  return { classifications, isLoadingClassifications: isLoading };
156
}
157
158
export function useLoadSkills(
159
  dispatch: DispatchType,
160
): {
161
  skills: Skill[];
162
  isLoadingSkills: boolean;
163
} {
164
  const skills = useSelector(getSkills);
165
  const isLoading = useSelector(getSkillsUpdating);
166
  useEffect((): void => {
167
    if (skills.length === 0 && !isLoading) {
168
      dispatch(fetchSkills());
169
    }
170
  }, [skills.length, isLoading, dispatch]);
171
  return { skills, isLoadingSkills: isLoading };
172
}
173